﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Icoders.MarketTracker.Core.MasterData;
using Icoders.MarketTracker.Core.Repository.MasterDataRepositories;
using Icoders.MarketTracker.Data.EF;

namespace Icoders.MarketTracker.Web.Lib.Services.Impl
{
    public class DropDownService : IDropDownService
    {
        private AppDataContext _ctx;
        private IZoneRepository _zoneRepository;
        private IAreaRepository _areaRepository;
        private ICountryRepository _countryRepository;
        private IUserRepository _userRepository;
        private ICityRepository _cityRepository;
        private IReasonRepository _reasonRepository;
        private IPointOfPurchaseRepository _pointOfPurchaseRepository;
        private IPointOfPurchaseTypeRepository _pointOfPurchaseTypeRepository;
        private IProductRepository _productRepository;
        private IFrsAssignRepository _fsrAssignRepository;
        private IProductBrandRepository _productBrandRepository;

        public DropDownService(AppDataContext ctx,IProductBrandRepository productBrandRepository, IZoneRepository zoneRepository, IAreaRepository areaRepository, ICountryRepository countryRepository, IUserRepository userRepository, ICityRepository cityRepository, IReasonRepository reasonRepository, IPointOfPurchaseRepository pointOfPurchaseRepository, IPointOfPurchaseTypeRepository pointOfPurchaseTypeRepository, IProductRepository productRepository, IFrsAssignRepository fsrAssignRepository)
        {
            _ctx = ctx;
            _zoneRepository = zoneRepository;
            _areaRepository = areaRepository;
            _countryRepository = countryRepository;
            _userRepository = userRepository;
            _cityRepository = cityRepository;
            _reasonRepository = reasonRepository;
            _pointOfPurchaseRepository = pointOfPurchaseRepository;
            _pointOfPurchaseTypeRepository = pointOfPurchaseTypeRepository;
            _productRepository = productRepository;
            _fsrAssignRepository = fsrAssignRepository;
            _productBrandRepository = productBrandRepository;
        }

        public DropDownService(AppDataContext ctx, IZoneRepository zoneRepository)
        {
            _ctx = ctx;
            _zoneRepository = zoneRepository;
        }

        public List<SelectListItem> GetZone(int areaId)
        {
           // return _ctx.TblZone.ToList().Where(p => p.AreaId ==areaId).Select(s => new SelectListItem{ Text=s.TblArea.Name+"-"+s.Name,Value=s.Id.ToString()}).ToList();
            return _zoneRepository.GetAll().Where(p => p.AreaId == areaId).OrderBy(o => o.Name).Select(s => new SelectListItem { Text = s.AreaName + "-" + s.Name, Value = s.Id.ToString() }).ToList();
        }
        public List<SelectListItem> GetAllZone()
        {
            //return _ctx.TblZone.ToList().Select(s => new SelectListItem { Text = s.TblArea.Name + "-" + s.Name, Value = s.Id.ToString() }).ToList();
            return _zoneRepository.GetAll().OrderBy(o => o.Name).Select(s => new SelectListItem { Text = s.AreaName + "-" + s.Name, Value = s.Id.ToString() }).ToList();
        }
        public List<SelectListItem> GetArea(int cityId)
        {
            //return _ctx.TblArea.ToList().Where(p => p.CityId == cityId).Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
            return _areaRepository.GetAll().Where(p => p.CityId == cityId).OrderBy(o => o.Name).Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
        }
        public List<SelectListItem> GetAllArea()
        {
            //return _ctx.TblArea.ToList().Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
            return _areaRepository.GetAll().OrderBy(o => o.Name).Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
        }
        public List<SelectListItem> GetAllClient()
        {
            return _ctx.TblClient.ToList().OrderBy(o => o.Name).Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
        }
        public List<SelectListItem> GetCity(int countyId)
        {
            //return _ctx.TblCity.ToList().Where(c => c.CountryId == countyId).Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
            return _cityRepository.GetAll().Where(c => c.CountryId == countyId).OrderBy(o => o.Name).Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
        }
        public List<SelectListItem> GetAllCity()
        {
           // return _ctx.TblCity.ToList().Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
            return _cityRepository.GetAll().OrderBy(o => o.Name).Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
        }
        public List<SelectListItem> GetCountry()
        {
            //return _ctx.TblCountry.ToList().Select(s => new SelectListItem{ Text=s.Name,Value=s.Id.ToString()}).ToList();
            return _countryRepository.GetAll().OrderBy(o => o.Name).Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
        }

        public List<SelectListItem> GetSupplier()
        {
            return _ctx.TblClient.ToList().OrderBy(o => o.Name).Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
        }

        public List<SelectListItem> GetReason()
        {
            //return _ctx.TblReason.ToList().Select(s => new SelectListItem{ Text=s.Name,Value=s.Id.ToString()}).ToList();
            return _reasonRepository.GetAll().OrderBy(o => o.Name).Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
        }

        public List<SelectListItem> GetSalesPersons()
        {
            //return _ctx.TblUser.ToList().Select(s => new SelectListItem{ Text=s.Username,Value=s.Id.ToString()}).ToList();
            return _userRepository.GetAll().Where(s => s.UserTypeId == (int)UserType.MobileAppUser).OrderBy(o => o.Username).Select(s => new SelectListItem { Text = s.Username, Value = s.Id.ToString() }).ToList();
        }

        public List<SelectListItem> GetPointofPurchase()
        {
            //return _ctx.TblPointOfPurchase.ToList().Select(s => new SelectListItem{ Text=s.Name,Value=s.Id.ToString()}).ToList();
            return _pointOfPurchaseRepository.GetAll().OrderBy(o => o.Name).Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
        }

        public List<SelectListItem> GetPointOfPurchaseTypes()
        {
           // return _ctx.TblPointOfPurchaseType.ToList().Select(s => new SelectListItem{ Text=s.Name,Value=s.Id.ToString()}).ToList();
            return _pointOfPurchaseTypeRepository.GetAll().OrderBy(o => o.Name).Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
        }


        public  List<SelectListItem> EnumToList<T>()
        {
            List<SelectListItem> enumValList = new List<SelectListItem>();
            Type enumType = typeof(T);

            // Can't use type constraints on value types, so have to do check like this
            if (enumType.BaseType != typeof(Enum))
                throw new ArgumentException("T must be of type System.Enum");

            Array enumValArray = Enum.GetValues(enumType);
            foreach (int val in enumValArray)
            {
                enumValList.Add(new SelectListItem{Text=Enum.Parse(enumType, val.ToString()).ToString(),Value=((int)val).ToString()});
            }
            return enumValList;
        }

        public List<SelectListItem> GetUserGroup()
        {
            return _ctx.TblUserGroup.ToList().OrderBy(o => o.Name).Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
        }

        public List<SelectListItem> GetProduct()
        {
            //return _ctx.TblProduct.ToList().Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
            return _productRepository.GetAll().OrderBy(o => o.Name).Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
        }

        public List<SelectListItem> GetFsrByArea(int areaId)
        {
           // return _ctx.TblUser.Where(s => s.UserTypeId == (int)UserType.MobileAppUser && s.TblFrsAssign.Where(m=>m.AreaId==areaId).Any()).ToList().Select(s => new SelectListItem { Text = s.Username, Value = s.Id.ToString() }).ToList();
            return _fsrAssignRepository.GetAll().Where(m => m.AreaId == areaId).OrderBy(o => o.Username).Select(s => new SelectListItem { Text = s.Username, Value = s.UserId.ToString() }).ToList();
        }
        public List<SelectListItem> GetPointofPurchaseByZone(int zoneId)
        {
            //return _ctx.TblPointOfPurchase.Where(x => x.ZoneId == zoneId).ToList().Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
            return _pointOfPurchaseRepository.GetAll().Where(x => x.ZoneId == zoneId).ToList().OrderBy(o => o.Name).Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
        }

        public List<SelectListItem> GetProductBrand()
        {
            // return _ctx.TblPointOfPurchaseType.ToList().Select(s => new SelectListItem{ Text=s.Name,Value=s.Id.ToString()}).ToList();
            return _productBrandRepository.GetAll().OrderBy(o => o.Name).Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() }).ToList();
        }
    }
}
